package admincow.servlet.becerro;

import java.io.IOException;
import java.text.DecimalFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admincow.dao.BecerroDAO;
import admincow.vo.BecerroVO;

/**
 * Servlet implementation class for Servlet: UsuarioInsertarServlet
 * 
 */


public class BecerroInsertarServlet extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public BecerroInsertarServlet() {
		super();
	}


	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}


	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 
		System.out.println("Enttrando a insertar becerro");
		String a = request.getParameter("RPbecerro");
		int v=a.length();
		if (v==4){
			System.out.println("Yes");

			BecerroDAO pao= new BecerroDAO();
			BecerroVO vi = pao.codigo(a);
			request.setAttribute("codigo", vi);

		     BecerroVO cod= (BecerroVO)request.getAttribute("codigo");        
	                   String r = cod.getRPbecerro();
	                   String m;
	                   System.out.println("recuperando");
	                   System.out.println(r);
	                   int nro;
	                   nro=Integer.parseInt(r);
	                   DecimalFormat sd= new DecimalFormat("0000");
	                   m=a+sd.format(nro);
						System.out.println("Pruebamelo");
						System.out.println(m);
						request.setAttribute("cobece", m);
						
			RequestDispatcher rd = request
					.getRequestDispatcher("/becerroNuevoo.jsp");
			rd.forward(request, response);
	
		}else{
			System.out.println("No");
				int b = Integer.parseInt(request.getParameter("idInseminacion"));
				int c = Integer.parseInt(request.getParameter("idParto"));
				String d = request.getParameter("rptoro");
				int e = Integer.parseInt(request.getParameter("idraza"));
				String f = request.getParameter("rpvaca");
				String g = request.getParameter("nombre");
				String h = request.getParameter("fech_nac");
				String i = request.getParameter("sexo");
				int j = Integer.parseInt(request.getParameter("registro"));
				String k = request.getParameter("fech_reg");
				String l = request.getParameter("estado");
				System.out.println("Paso todo bien hasta aca");
				BecerroVO vo = new BecerroVO();
				vo.setRPbecerro(a);
				vo.setIdinseminacion(b);
				vo.setIdparto(c);
				vo.setRPtoro(d);
				vo.setIdraza(e);
				vo.setRPvaca(f);
				vo.setNombre(g);
				vo.setFech_nac(h);
				vo.setSexo(i);
				vo.setRegistro(j);
				vo.setFech_reg(k);
				vo.setEstado(l);
				BecerroDAO dao = new BecerroDAO();
				dao.insertar(vo);

				RequestDispatcher rd = request
						.getRequestDispatcher("/becerroBuscar.jsp");
				rd.forward(request, response);
		
		}
				
	}
	
}